package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lhh
 * @ClassName SpuInfoController
 * @date 2022 2022/12/1
 */
@Api(tags = "spu接口")
@RestController
@RequestMapping("admin/product")
public class SpuInfoController {

    @Resource
    SpuInfoService spuInfoService;

    @Resource
    BaseSaleAttrService baseSaleAttrService;

    @Resource
    SpuSaleAttrService spuSaleAttrService;


    @Resource
    SpuImageService spuImageService;

    @ApiOperation(value = "根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> list = spuImageService.lambdaQuery().eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(list);
    }

    @ApiOperation(value = "获取spu分页列表")
    @GetMapping("{page}/{limit}")
    public Result spuInfoList(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              @Param("category3Id") Long category3Id){
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        spuInfoPage=spuInfoService.getList(spuInfoPage,category3Id);
        return Result.ok(spuInfoPage);
    }

    @ApiOperation(value = "获取销售属性")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> list = baseSaleAttrService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfoService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation(value = "根据spuId获取销售属性")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> list=spuSaleAttrService.spuSaleAttrList(spuId);
        return Result.ok(list);
    }

}
